package date0708;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import cn.edu.ecut.util.DatabaseType;
import cn.edu.ecut.util.JdbcHelper;

public class StudentTest {

	public static void main(String[] args) throws SQLException, IOException {
		
		JdbcHelper h = JdbcHelper.getInstance(DatabaseType.MYSQL);
		final String select = "SELECT id ,name,gender,birthdate FROM t_users";
		PreparedStatement s = h.prepare( select );
		ResultSet rs = s.executeQuery(); 
		h.traverse( rs );
		
		ResultSetMetaData rsmd = rs.getMetaData();
		final int columnCount = rsmd.getColumnCount(); // 获取查询结果中的列数 ( 不一定是表中的列数 )
		System.out.println( "查询结果中包含 " + columnCount + " 列");
		
		for( int i = 1 ; i <= columnCount ; i++ ) {
			// 获得第 i 列的 标签名 ( 列名 或 列别名 )
			String label = rsmd.getColumnLabel( i ); // 如果指定果列的别名，就返回列的别名
			label = label.toLowerCase();
			String typeName = rsmd.getColumnTypeName( i );
			System.out.println( label + " , " + typeName );
		}
		
		File file = new File("D:/train/Workspace/Project5/src/date0708/Tuser.java");
		OutputStream out = new FileOutputStream(file);
		Charset ch=Charset.forName("utf-8");
		OutputStreamWriter osw = new OutputStreamWriter(out,ch);
		
		osw.write("package date0708;"+"\n");
		osw.write("\n");
		osw.write("import java.time.LocalDate;"+"\n");
		osw.write("public class Tuser{" + "\n");
		for(int i=1;i<=columnCount;i++) {
			osw.write("\t"+"private " );
			if(rsmd.getColumnTypeName( i )=="INT")
				osw.write(rsmd.getColumnTypeName( i ).toLowerCase()+" ");
			else if(rsmd.getColumnTypeName( i )=="VARCHAR"||rsmd.getColumnTypeName( i )=="CHAR")
				osw.write("String ");
			else if(rsmd.getColumnTypeName( i )=="DATE")
				osw.write("LocalDate ");
			
			osw.write(rsmd.getColumnLabel( i )+";"+"\n");
			
			osw.write("      public ");
			if(rsmd.getColumnTypeName( i )=="INT")
				osw.write(rsmd.getColumnTypeName( i ).toLowerCase()+" ");
			else if(rsmd.getColumnTypeName( i )=="VARCHAR"||rsmd.getColumnTypeName( i )=="CHAR")
				osw.write("String ");
			else if(rsmd.getColumnTypeName( i )=="DATE")
				osw.write("LocalDate ");
			osw.write("get"+rsmd.getColumnLabel( i ).substring(0,1).toUpperCase()+rsmd.getColumnLabel( i ).substring(1)+"()"+"{" + "\n");
			osw.write("        return this."+rsmd.getColumnLabel( i ) +";"+ "\n"+"      }" + "\n");
			
			osw.write("      public void");
			if(rsmd.getColumnTypeName( i )=="INT") {
				osw.write(" set"+rsmd.getColumnLabel( i ).substring(0,1).toUpperCase()+rsmd.getColumnLabel( i ).substring(1));
				osw.write("( int "+rsmd.getColumnLabel( i )+")"+"{" + "\n");
			}
			else if(rsmd.getColumnTypeName( i )=="VARCHAR"||rsmd.getColumnTypeName( i )=="CHAR") {
				osw.write(" set"+rsmd.getColumnLabel( i ).substring(0,1).toUpperCase()+rsmd.getColumnLabel( i ).substring(1));
				osw.write("( String "+rsmd.getColumnLabel( i )+")"+"{" + "\n");
			}
			else if(rsmd.getColumnTypeName( i )=="DATE") {
				osw.write(" set"+rsmd.getColumnLabel( i ).substring(0,1).toUpperCase()+rsmd.getColumnLabel( i ).substring(1));
				osw.write("( LocalDate "+rsmd.getColumnLabel( i )+")"+"{" + "\n");
			}
			osw.write("        this."+rsmd.getColumnLabel( i ) +"="+rsmd.getColumnLabel( i ) +";"+"\n"+"      }" + "\n");
			
		}
		osw.write("}");
		
		osw.close();
		out.close();

		h.close( rs );
		h.close( s );
		h.release();
	}

}
